import {
  getHistory,
  addHistory,
  cleanAllHistory,
  cleanHistory,
} from "@/Api/history";
const state = {
  historyList: [],
};
const mutations = {
  ADD_HISTORYLIST(state, list) {
    state.historyList = list;
  },
  UPDATE_HISTORY(state, data) {
    state.historyList[data.index1].children.splice(data.index2, 1);
    if(state.historyList[data.index1].children.length==0)
      state.historyList.splice(data.index1,1)
  },
};
const actions = {
  deleteHistory(context, data) {
    let id = context.state.historyList[data.index1].children[data.index2].hisId;
    console.log(id)
    cleanHistory(id).then((res) => {
      console.log(res);
      if (res.data.status == 200) {
        context.commit("UPDATE_HISTORY", data);
      }
    });
  },
  async cleanAllHistory({ commit }) {
    let result = await cleanAllHistory();
    console.log(result);
    if (result.data.status == 200) {
      commit("ADD_HISTORYLIST", []);
      return "success";
    } else return "error";
  },
  async getHistory({ commit }) {
    let result = await getHistory();
    console.log(result);
    if (result.data.status == 200 || result.data.status == 203) {
      commit("ADD_HISTORYLIST", result.data.data);
      return "success";
    } else return "error";
  },
  // 传入书籍id
  addHistory({ commit }, id) {
    addHistory(id);
  },
};
const getters = {
  historyList(state) {
    return state.historyList || [];
  },
};
export default {
  state,
  mutations,
  actions,
  getters,
};
